package hnevc.com.gittestdemo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;

public class MainActivity extends AppCompatActivity {
    //定义按钮变量
    Button btnLogin;
    EditText etUsername;
    EditText etPassword;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //将buttun控件赋值
        etPassword = (EditText) findViewById(R.id.etPassword);
        etUsername = (EditText) findViewById(R.id.etUsername);
        btnLogin = (Button) findViewById(R.id.btnLogin);
        btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //实现登录功能
                //获取用户输入信息
                String username = etUsername.getText().toString();
                String password = etPassword.getText().toString();
                //创建线程
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        //访问网络，提交请求，并获取结果
                        try {
                            //构建访问的网址和传递参数
                            String urlstr="http://10.1.46.91:8080/RestaurantServer/user/doLogin3";
                            urlstr+="?userName=tom&userPassword=123";
                            //构建URL对象
                            URL url=new URL(urlstr);
                            //使用URL对象创建连接对象
                            HttpURLConnection conn= (HttpURLConnection) url.openConnection();
                            conn.setRequestMethod("GET");

                            //获取数据
                            InputStream inputStream=conn.getInputStream();
                            //读取字节流，将其装换喂字符串data
                            byte[] bytes=new byte[1024];
                            StringBuffer sb=new StringBuffer();
                            int len=0;
                            while ((len =inputStream.read(bytes))!=-1){
                                String s=new String(bytes,0,len);
                                sb.append(s);
                            }
                            final  String data =sb.toString();

                            //在UI线程中显示一个提示框
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(MainActivity.this,data,Toast.LENGTH_LONG).show();
                                }
                            });

                        }  catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }).start();
            }
        });
    }
}
